package com.huel.fragment;

import android.Manifest;
import android.app.Dialog;
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huel.bean.Item;
import com.huel.bean.Phone;
import com.huel.components.MyPhoneRecyclerViewAdapter;
import com.huel.components.RecyclerItemClickListener;
import com.huel.service.ContactUtils;
import com.huel.taobaostore.MainActivity;
import com.huel.taobaostore.R;

import java.util.ArrayList;
import java.util.List;

import static android.content.pm.PackageManager.*;

public class PhoneFragment extends Fragment {
    private static String[] PERMISSIONS_STORAGE = {//动态申请权限列表
//            "android.permission.WRITE_CONTACTS",
            "android.permission.READ_CONTACTS"};//读通讯录权限的权限名


    private View contextView;
    private MyPhoneRecyclerViewAdapter myPhoneRecyclerViewAdapter;
    private RecyclerView recyclerView;
    private List<Phone> mList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        contextView = inflater.inflate(R.layout.fragment_phone, container, false);
        //获取控件
        mList = new ArrayList<>();

        //获取数据源


        checkPermissionAllGranted();


        initLayout();


        //设置内部监听事件  查看联系人具体信息
       recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
           @Override
           public void onItemClick(View view, final int position) {
               //直接弹窗 显示信息
                ImageView show =view.findViewById(R.id.show);

               show.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                       View view01 = LayoutInflater.from(getActivity()).inflate(R.layout.show_dialog, null);
                       TextView name = view01.findViewById(R.id.name);
                       TextView tel = view01.findViewById(R.id.tel);
                       TextView sure = view01.findViewById(R.id.sureBtn);
                       name.setText("姓名："+mList.get(position).getName());
                       tel.setText("手机："+mList.get(position).getTel());
                       final Dialog dialog = builder.create();
                       dialog.show();
                       dialog.getWindow().setContentView(view01);
                       dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                       sure.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {
                               dialog.dismiss();
                           }
                       });
                   }

               });
           }
       }));





        return contextView;
    }

    public void initLayout() {
        myPhoneRecyclerViewAdapter = new MyPhoneRecyclerViewAdapter(mList);
        recyclerView = contextView.findViewById(R.id.rlv01);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setAdapter(myPhoneRecyclerViewAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    /**
     * 检查是否拥有指定的所有权限
     */
    private void checkPermissionAllGranted() {
        if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            // 只要有权限没有被授予, 则直接申请  //为什么没有弹窗？？？
            requestPermissions(new String[]{android.Manifest.permission.READ_CONTACTS}, 66);
        } else {
            mList= ContactUtils.getAllContacts(getActivity());
        }
    }
    //回调 询问结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 66) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //权限申请成功
                Toast.makeText(getActivity(), "获取联系人的权限成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "获取联系人的权限申请失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
